<html>
<head><meta charset="utf-8"><title>Span::shrink_to_hi - 1 · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html">Span::shrink_to_hi - 1</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219784447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219784447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219784447">(Dec 13 2020 at 22:12)</a>:</h4>
<p>Is there a way to <code>Span::shrink_to_hi</code> and subtract one? <code>Span::shrink_to_hi</code> is returning a span that is one character past where I want it.</p>



<a name="219784454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219784454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219784454">(Dec 13 2020 at 22:12)</a>:</h4>
<p>Should I use <code>with_hi</code> and manipulate the <code>BytePos</code> directly?</p>



<a name="219793531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219793531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219793531">(Dec 14 2020 at 02:13)</a>:</h4>
<p>Can you elaborate on your use case?</p>



<a name="219793794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219793794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219793794">(Dec 14 2020 at 02:21)</a>:</h4>
<p>Sure!</p>



<a name="219793802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219793802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219793802">(Dec 14 2020 at 02:21)</a>:</h4>
<p>This is the PR: <a href="https://github.com/rust-lang/rust/issues/80017">#80017</a></p>



<a name="219793854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219793854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219793854">(Dec 14 2020 at 02:22)</a>:</h4>
<p>I want to suggest adding <code>, ..</code> for code like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">S</span><span class="p">(</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">f32</span><span class="p">);</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">S</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">S</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>But using <code>shrink_to_hi</code> yields this code after applying the suggestion:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">S</span><span class="p">(</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">f32</span><span class="p">);</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">S</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mf">1.0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">S</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="w"> </span><span class="o">..</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>So I want <code>shrink_to_hi() - 1</code>.</p>



<a name="219813378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219813378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219813378">(Dec 14 2020 at 08:50)</a>:</h4>
<p>does <code>span.with_lo(span.hi() - 1)</code> and then <code>span.shrink_to_lo()</code> work?</p>



<a name="219815157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219815157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219815157">(Dec 14 2020 at 09:10)</a>:</h4>
<p>Shouldn’t <code>- 1</code> be <code>- BytePos(1)</code>?</p>



<a name="219894525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219894525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219894525">(Dec 14 2020 at 20:00)</a>:</h4>
<p>I was thinking of <code>span.with_hi(span.hi() - BytePos(1)).shrink_to_hi()</code>, but yeah, probably it will work. I guess I can assume that <code>hi()</code> is greater than zero since it shouldn't be at the beginning of a file.</p>



<a name="219894554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219894554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219894554">(Dec 14 2020 at 20:01)</a>:</h4>
<p>I just wasn't sure if there was a pre-existing method for doing this, so that's why I asked :)</p>



<a name="219895159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219895159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219895159">(Dec 14 2020 at 20:06)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Span.3A.3Ashrink_to_hi.20-.201/near/219894525">said</a>:</p>
<blockquote>
<p>I was thinking of <code>span.with_hi(span.hi() - BytePos(1)).shrink_to_hi()</code>, but yeah, probably it will work. I guess I can assume that <code>hi()</code> is greater than zero since it shouldn't be at the beginning of a file.</p>
</blockquote>
<p>hmm, that might not be true for dummy spans</p>



<a name="219895371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219895371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219895371">(Dec 14 2020 at 20:09)</a>:</h4>
<p>Ugh</p>



<a name="219895464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219895464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219895464">(Dec 14 2020 at 20:10)</a>:</h4>
<p>So should I check if it's <code>DUMMY_SP</code> and only do the shrinking if it's not?</p>



<a name="219895504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219895504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219895504">(Dec 14 2020 at 20:10)</a>:</h4>
<p>I would expect so</p>



<a name="219895508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Span%3A%3Ashrink_to_hi%20-%201/near/219895508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Span.3A.3Ashrink_to_hi.20-.201.html#219895508">(Dec 14 2020 at 20:10)</a>:</h4>
<p>haven't tested though</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>